package com.china08.yxyapi.api.temp;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.model.YxyApiRespModel;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = { "V01", "test" })
@CrossOrigin
@RestController
public class TestRestTemplateProformance extends YxyApiBaseController {
    private static final Logger LOGGER = LoggerFactory.getLogger(TestRestTemplateProformance.class);

    @Autowired
    private RestTemplate restTemplate;

    @Value(value = "${yx.rest.api.yx.path}")
    private String yxyApiYxUrl;

    /**
     * 
     * @param request
     * @param response
     * @return
     */
    @ApiOperation("test SpringRestTemplate proformance")
    @RequestMapping(value = "/api_yw/temp/test/rest", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void process(HttpServletRequest request, HttpServletResponse response) {
        try {
            HttpHeaders headers = new HttpHeaders();
            MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
            headers.setContentType(type);
            headers.add("Accept", MediaType.APPLICATION_JSON_UTF8_VALUE.toString());

            Map<String, Object> map = new HashMap<String, Object>();
            map.put("mobile", "15901025670");
            Long beginrest = System.currentTimeMillis();
            LOGGER.info("test rest: " + beginrest);
            String mobile = "15901025670";
            HttpEntity<Map<String, Object>> formEntity = new HttpEntity<Map<String, Object>>(map, headers);
            ResponseEntity<RespModel> responseEntity = restTemplate
                    .getForEntity(yxyApiYxUrl + "/comm/user/moblie" + "?mobile={mobile}", RespModel.class, mobile);
            if (responseEntity != null && responseEntity.getBody() != null) LOGGER.info(responseEntity.getBody().email);
            Long endrest = System.currentTimeMillis();
            LOGGER.info("test rest: " + endrest);
            LOGGER.info("rest consumer:(millis)" + (endrest - beginrest));
        } catch (Exception e) {
            LOGGER.info("getMomentForSchool方法执行错误：");
        }

    }

    protected static class RespModel extends YxyApiRespModel {
        public Integer gender;
        public String nickName;
        public String email;
        public String introduction;
        public String personalSignature;
    }
}
